package settings;

import de.app.badresden.R;
import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class SeekBarPreference extends DialogPreference implements
		SeekBar.OnSeekBarChangeListener {

	private static final String androidns = "http://schemas.android.com/apk/res/android";

	private SeekBar seekBar;
	private TextView splashText, valueText;
	private Context context;

	private String dialogMessage, einheit;
	private int defaultVal, maxVal, mValue = 0;

	public SeekBarPreference(Context context, AttributeSet attrs) {

		super(context, attrs);
		this.context = context;
		
		dialogMessage = context.getString(R.string.settings_datenstundenplansummary);
		einheit = context.getString(R.string.settings_datenstundenplaneinheit);
		attrs.getAttributeValue(androidns, "dialogMessage");
		
		defaultVal = attrs.getAttributeIntValue(androidns, "defaultValue", 3);
		maxVal = attrs.getAttributeIntValue(androidns, "max", 100);
		
		setNegativeButtonText(null);
		
		setDialogTitle(R.string.settings_datenstundenplantitle);
	}

	@Override
	protected View onCreateDialogView() {
		
		LinearLayout.LayoutParams params;
		LinearLayout layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setPadding(20, 20, 20, 20);
		
		splashText = new TextView(context);
		if (dialogMessage != null) splashText.setText(dialogMessage);
		layout.addView(splashText);

		valueText = new TextView(context);
		valueText.setGravity(Gravity.CENTER_HORIZONTAL);
		valueText.setTextSize(32);
		params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		layout.addView(valueText, params);

		seekBar = new SeekBar(context);
		seekBar.setOnSeekBarChangeListener(this);
		layout.addView(seekBar, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

		if (shouldPersist()) mValue = getPersistedInt(defaultVal);

		seekBar.setMax(maxVal);
		seekBar.setProgress(mValue);
		
		return layout;
	}

	@Override
	protected void onBindDialogView(View v) {

		super.onBindDialogView(v);
		seekBar.setMax(maxVal);
		seekBar.setProgress(mValue);
		
		String t = String.valueOf(mValue);
		if (mValue == 1) {
			String einheit_ez = einheit.substring(0, einheit.length()-1);
			valueText.setText(t + " " + einheit_ez);
		}
		else
			valueText.setText(t + " " + einheit);
	}

	@Override
	protected void onSetInitialValue(boolean restore, Object defaultValue) {

		super.onSetInitialValue(restore, defaultValue);
		if (restore) mValue = shouldPersist() ? getPersistedInt(defaultVal) : 0;
		else
			mValue = (Integer) defaultValue;
	}

	public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) {

		String t = String.valueOf(value);
		if (value == 1) {
			String einheit_ez = einheit.substring(0, einheit.length()-1);
			valueText.setText(t + " " + einheit_ez);
		}
		else
			valueText.setText(t + " " + einheit);
		if (shouldPersist()) persistInt(value);
		callChangeListener(new Integer(value));
	}

	public void onStartTrackingTouch(SeekBar seek) {

	}

	public void onStopTrackingTouch(SeekBar seek) {

	}

	public void setMax(int max) {

		maxVal = max;
	}

	public int getMax() {

		return maxVal;
	}

	public void setProgress(int progress) {

		mValue = progress;
		if (seekBar != null) seekBar.setProgress(progress);
	}

	public int getProgress() {

		return mValue;
	}
}